﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace System.Web.Mvc
{
    public static class MyHtmlHelper
    {
        public static MvcHtmlString MyTextBox<TModel,Type>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, Type>> expression, string name,bool IsNeedCheck)
        {
            StringBuilder htmlSb = new StringBuilder();
            htmlSb.Append("<div class=\"form-group\">");
            htmlSb.Append("<label class=\"col-sm-2 control-label no-padding-right\" for=\"form-field-1\">" + name + " </label>");
            htmlSb.Append("<div class=\"col-sm-10\">");
            htmlSb.Append(htmlHelper.TextBoxFor(expression));
            if (IsNeedCheck)
            {
                htmlSb.Append("<span class=\"help-inline col-xs-12 col-sm-7 has-error\">");
                htmlSb.Append("<span class=\"middle\">");
                htmlSb.Append(htmlHelper.ValidationMessageFor(expression));
                htmlSb.Append("</span></span>");
            }
            htmlSb.Append("</div></div>");

            return MvcHtmlString.Create(htmlSb.ToString());
        }
        /// <summary>
        /// 为由指定表达式表示的每个数据字段的验证错误消息返回对应的 HTML 标记。
        /// </summary>
        /// <typeparam name="TModel">模型的类型。</typeparam>
        /// <typeparam name="TProperty">属性的类型。</typeparam>
        /// <param name="htmlHelper">此方法扩展的 HTML 帮助器实例。</param>
        /// <param name="expression"> 一个表达式，用于标识包含要呈现的属性的对象。</param>
        /// <returns>如果该属性或对象有效，则为一个空字符串；否则为一个包含错误消息的 span 元素。</returns>
        public static MvcHtmlString MyValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
        {
            StringBuilder htmlSb = new StringBuilder();
            htmlSb.Append("<span class=\"help-inline has-error\">");
            htmlSb.Append("<span class=\" help-block col-xs-12 col-sm-reset inline\">");
            htmlSb.Append(htmlHelper.ValidationMessageFor(expression));
            htmlSb.Append("</span>");
            htmlSb.Append("</span>");
            return MvcHtmlString.Create(htmlSb.ToString());
        }
    }
}